import logging

import pytest

from resource.pom.launcher import Launcher
from resource.pom.settings import Settings
from utils.manager import Direction


class Test:
    app = Settings

    @pytest.mark.parametrize('setup_teardown', [app.bundle_name], indirect=True)
    def test(self, setup_teardown, device):
        logging.info('步骤1.点击设置-用户和帐号-当前登录-添加用户')
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.dirc_fling(Direction.Down)
        device.click(*self.app.users_accounts_item)
        device.refresh_layout()
        device.click_element(device.get_element_by_text('当前登录'))
        device.refresh_layout()
        device.click_element(device.get_element_by_text('添加用户'))
        logging.info('预期结果1.可正常进入到添加用户界面')
        device.refresh_layout()
        device.assert_text_exist('添加新用户')
        device.assert_type_exist('TextInput')
        device.assert_text_exist('取消')
        device.assert_text_exist('添加')

        logging.info('步骤2.点击输入框输入昵称，点击添加')
        device.click_element(device.get_element_by_type('TextInput'))
        logging.info('预期结果2.点击输入框可唤醒键盘，输入昵称，点击添加显示"切换到新用户吗?"弹窗')
        device.refresh_layout()
        device.assert_soft_keyboard_on()
        text_input = device.get_element_by_type('TextInput')
        tx, ty = device.center_of_element(text_input)
        device.input_text(tx, ty, 'test')
        device.wait(1)
        device.click_element(device.get_element_by_text('添加'))
        device.wait(3)
        device.refresh_layout()
        device.assert_text_exist('切换到新用户吗？')
        device.assert_text_exist('取消')
        device.assert_text_exist('切换')

        logging.info('步骤3.点击切换')
        device.click_element(device.get_element_by_text('切换'))
        logging.info('预期结果3.点击切换可正常切换到新用户，新用户应用功能正常')
        device.wait(8)
        device.unlock()
        device.assert_current_window(Launcher.entry_view)
        device.start_ability(self.app.bundle_name, self.app.ability_name)
        device.dirc_fling(Direction.Down)
        device.click(*self.app.users_accounts_item)
        device.refresh_layout()
        device.assert_text_exist('test')
        try:
            device.click_element(device.get_element_by_text('当前登录'))
            device.click(360, 1120)
            device.click(520,1117)
            device.wait(5)
        except:
            pass